//
//  BaseModel.m
//  WXMovie
//
//  Created by apple on 16/4/8.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "BaseModel.h"

@implementation BaseModel

- (id)initWithDic:(NSDictionary *)dic {
    
    if (self = [super init]) {
       
        [self buildRelationship:dic];
    }
    return self;
}

- (SEL)changeValueForKey:(NSString *)key {
    
    NSString *keyStr = [key capitalizedString];
    
    NSString *setStr = nil;
    
    if ([key isEqualToString:@"id"]) {
        setStr = [NSString stringWithFormat:@"set%@%@:", NSStringFromClass([self class]),keyStr];
    }else {
        setStr = [NSString stringWithFormat:@"set%@:", keyStr];
        
    }
    
    return NSSelectorFromString(setStr);
    
}

- (void)buildRelationship:(NSDictionary *)dic {
    
    NSArray *allKeys = [dic allKeys];
    
    for (NSString *key in allKeys) {
        
        SEL method = [self changeValueForKey:key];
        id value = dic[key];
        if ([self respondsToSelector:method]) {
            [self performSelector:method withObject:value];
        }
        
    }
}


@end
